package com.fs.book.web;

import com.fs.book.entity.Book;
import com.fs.book.entity.CartItem;
import com.fs.book.entity.User;
import com.fs.book.mapper.CartMapper;
import com.fs.book.service.BookService;
import com.fs.book.service.CartService;
import com.fs.book.vo.BookVO;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CartService cartService;
    @GetMapping("list")
    @ResponseBody
    public Map queryPage(BookVO bookVO) throws  Exception{
        Map map = new HashMap();
        Page<Book> page =  bookService.queryByPage(bookVO);
        map.put("status",200);
        map.put("pageIndex",page.getPageNum());
        map.put("pageSize",page.getPageSize());
        map.put("records",page.getResult());
        map.put("total",page.getTotal());
        map.put("pages",page.getPages());
        return map;
    }
    @GetMapping("queryById")
    @ResponseBody
    public Book queryById(Integer bid) throws  Exception{
        return bookService.queryById(bid);
    }
    // 获取购物车数据
    @GetMapping("/getCartData")
    @ResponseBody
    public List<CartItem> getCartData(HttpSession session) {
        return cartService.getCartData();
    }
    @GetMapping("/{bid}")
    public ResponseEntity<Book> getBookById(@PathVariable Integer bid) {
        try {
            Book book = bookService.queryById(bid);
            if (book != null) {
                return new ResponseEntity<>(book, HttpStatus.OK);
            } else {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    //添加购物车
    @PostMapping("/addToCart")
    @ResponseBody
    public String addToCart(@RequestBody CartItem cartItem, HttpSession session) {
        try {
            cartService.addToCart(cartItem);
            System.out.println(123);
            return "添加购物车成功";
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
    // 从购物车移除商品
    @PostMapping("/removeFromCart")
    @ResponseBody
    public String removeFromCart(@RequestParam("cartItemId") Integer cartItemId, HttpSession session) {
        try {
            cartService.removeFromCart(cartItemId);
            return "删除成功";
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
    // 清空购物车
    @PostMapping("/clearCart")
    @ResponseBody
    public String clearCart() {
        try {
            cartService.clearCart();
            return "您的购物车已清空";
        } catch (Exception e) {
            return "错误信息: " + e.getMessage();
        }
    }
}
